/*
 Copyright 2010 Wissen System Pvt. Ltd. All rights reserved.

 */
package com.wissen.giskard.client.widget;

import java.util.List;

import javax.persistence.Table;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.wissen.giskard.client.payroll.services.ColumnData;

/**
 * @author Sagar Varpe
 * 
 *         Create Date : 06-Aug-2010 5:54:12 PM This is a Comman widget for tables used by all modules
 */
public class DynamicTableWidget extends Composite {

    private Grid            grid        = new Grid(); 

    private VerticalPanel   panel       = new VerticalPanel();

    private HorizontalPanel buttonPanel = new HorizontalPanel();

    private Button          addButton   = new Button("Add");

    private Button          delButton   = new Button("Delete");

    private Button          editButton  = new Button("Edit");

    private final int       rowCount    = 10;

    private int             selectedRow;
    private int noOfRows;
    
    private CheckBox      paidCheck     = new CheckBox();

    public DynamicTableWidget(String[] header, List<ColumnData[]> colmData) {

    	noOfRows= colmData.size();
        initTable(header);
        initContent(colmData);
        initButtons();
        panel.add(grid);
        panel.add(buttonPanel);

        initWidget(panel);

    }

    private void initContent(List<ColumnData[]> colmData) {
        // TODO Auto-generated method stub
    	int i,j=1;
		try{
		
			
			for(ColumnData[] column:colmData) {
				
				for(i=0;i<5;i++) {
					
					
					grid.setText(j, i, column[i].getValues());
					grid.getCellFormatter().addStyleName(j, i, "dynamicTableContent");
				}
				j++;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
    }

    private void initButtons() {

        buttonPanel.add(addButton);
        buttonPanel.add(delButton);
        delButton.setEnabled(false);
        buttonPanel.add(editButton);
        editButton.setEnabled(false);
        addButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                addButtonClick();

            }          
        });
        delButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                delButtonClick();
            }

        });
        editButton.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                editButtonClick();
            }

            
        });

    }
    private void addButtonClick() {
        // TODO Auto-generated method stub
        showDialog("Add");
        
    }

    private void delButtonClick() {
    	 // TODO Auto-generated method stub
        VerticalPanel verticalPanel = new VerticalPanel();
        HorizontalPanel horizontalPanel= new HorizontalPanel();
        Button okButton = new Button("Ok");
        Button cancelButton= new Button("Cancel");
        final DialogBox dialogBox = new DialogBox();
        dialogBox.center();        
        dialogBox.setText("Giskard Delete Record");
        verticalPanel.add(new Label(" Are you sure ! You want to Delete this record."));
        horizontalPanel.add(okButton);
        horizontalPanel.add(cancelButton);
        
        verticalPanel.add(horizontalPanel);
        dialogBox.add(verticalPanel);
        dialogBox.show();
        cancelButton.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                // TODO Auto-generated method stub
                dialogBox.removeFromParent();
            }
        });
        
    }
    private void editButtonClick() {
    	 showDialog("Edit");
        
    }
private void showDialog(String operation) {
        
        int noColumn=grid.getColumnCount();
        Button okButton = new Button("Ok");
        Button cancelButton = new Button("Cancel");
        
        final DialogBox dialogBox = new DialogBox();
        
        VerticalPanel verticalPanelLabel= new VerticalPanel();
        VerticalPanel verticalPanelText = new VerticalPanel();
        
        HorizontalPanel horizontalPanel = new HorizontalPanel();
        HorizontalPanel horizontalPanelButton = new HorizontalPanel();
        
        
        
        dialogBox.center();
        dialogBox.setText("Giskard "+operation+" Record");
         for( int i=0;i<noColumn;i++)
         {
             Label label = new Label(grid.getText(0, i)+" : ");
             label.addStyleName("dialogLabel");
             verticalPanelLabel.add(label);
          
             TextBox textBox = new TextBox();
             textBox.addStyleName("dialogTextBox");
             if(operation.equals("Edit"))
                 textBox.setText(grid.getText(selectedRow, i));
             verticalPanelText.add(textBox);
             
         }
         horizontalPanelButton.add(okButton);
         horizontalPanelButton.add(cancelButton);
         verticalPanelText.add(horizontalPanelButton);
         horizontalPanel.add(verticalPanelLabel);
         horizontalPanel.add(verticalPanelText);
         dialogBox.add(horizontalPanel);    
        dialogBox.show();
        
        cancelButton.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                dialogBox.removeFromParent();
                
            }
        });

    }


    //Set Up The Header Row
    public void initTable(String[] headers) {
        grid.resize(rowCount + 1, headers.length);
        grid.addStyleName("dynamicTableWidget");
        for (int i = 0, n = headers.length; i < n; i++) {
            //columnData = (ColumnData) headers.get(i);
            grid.setText(0, i, headers[i]);
            grid.getCellFormatter().addStyleName(0, i, "dynamicTableHeader");

        }
        grid.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
            	int tempRow=grid.getCellForEvent(event).getRowIndex();
                if ( tempRow!= 0 && tempRow<=noOfRows) {
                    grid.getRowFormatter().removeStyleName(selectedRow, "selectedRow");
                    selectedRow = grid.getCellForEvent(event).getRowIndex();
                    grid.getRowFormatter().setStyleName(selectedRow, "selectedRow");
                    editButton.setEnabled(true);
                    delButton.setEnabled(true);
                }

                //Window.alert(" " + grid.getCellForEvent(event).getCellIndex() + " " + grid.getCellForEvent(event).getRowIndex());
            }
        });
    }

}
